package com.fudan.backend.controller;

import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.Activity;
import com.fudan.backend.service.ActivityService;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    ActivityService activityService;

    @PostMapping("/launch")
    public R launchActivity(@RequestBody Activity activity){
        RetCode retCode = activityService.addActivity(activity);
        return new R(retCode);
    }

    @GetMapping("/all")
    public R getAllActivity(int current, int size) {
        PageInfo<Activity> page = activityService.getAllActivity(current, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.ACTIVITY_SUCCESS.getCode(), RetCode.ACTIVITY_SUCCESS.getMessage(), RetCode.ACTIVITY_SUCCESS.getMessage(), map);
    }

    @GetMapping("/ongoing")
    public R getAllOngoingActivity(int current, int size) {
        PageInfo<Activity> page = activityService.getAllOngoingActivity(current, size);
        HashMap<String, Object> map = new HashMap<>();
        map.put("activityList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.ACTIVITY_SUCCESS.getCode(), RetCode.ACTIVITY_SUCCESS.getMessage(), RetCode.ACTIVITY_SUCCESS.getMessage(), map);
    }

    @GetMapping("/info/{activityId}")
    public R getActivityByActivityId(@PathVariable("activityId") String activityId){
        Activity activity = activityService.getActivityByActivityId(activityId);
        return new R(RetCode.ACTIVITY_SUCCESS,activity);
    }
}
